package com.yn.courseselectionsystem.controller;

import com.yn.courseselectionsystem.pojo.Appointment;
import com.yn.courseselectionsystem.pojo.Course;
import com.yn.courseselectionsystem.pojo.User;
import com.yn.courseselectionsystem.server.SocketServer;
import com.yn.courseselectionsystem.service.impl.AppointmentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2021/1/13 18:43
 * Role of: 预约控制层
 * @version: v1.0
 */
//@SessionAttributes(value = {"user"})加上这个还能访问到那边会话的user的,
// 应该说整个程序 只有这一个@SessionAttributes
@SessionAttributes(value = {"user","hintMsg"})//hintMsg:有人退课后会通知
@Controller
public class AppointmentController {
    //预约
    @Resource
    private AppointmentServiceImpl appointmentService;

    @Resource
    private SocketServer socketServer;//站内信

    /**
     * 预约课程
     * @param course
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping("/studentManagement/succPage/appointment/addAppointment")
    public Integer addAppointment(Course course, Model model, HttpSession session){
        User user= (User) model.getAttribute("user");
//        User user= (User) session.getAttribute("user");
        Appointment appointment=new Appointment();
        appointment.setCourseId(course.getId());
        appointment.setTakeTheCourseWin(0);
        appointment.setUserId(user.getId());
        /*System.out.println(appointment.toString());*/
        /*appointmentService.addAppointment(appointment)*/
        socketServer.sendMessage("恭喜你预约课程"+course.getName()+"成功",user.getId()+"");
        return appointmentService.addAppointment(appointment);
    }

    /**
     * 监测是否预约
     * @param course
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping("/studentManagement/succPage/appointment/isAppointment")
    public Integer isAppointment(Course course, Model model){
        int num=0;
        User user= (User) model.getAttribute("user");
        //查询  此用户的这个课程是否预约
        Appointment appointment=appointmentService.selAppointment(user.getId(),course.getId());
        if(appointment!=null){
            num=1;
        }
        return num;
    }
}
